package com.kws.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kws.common.core.domain.R;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.info.QueryDictDataInfo;
import com.kws.system.domain.vo.CreateDictDataRq;
import com.kws.system.domain.vo.QueryDictDataRq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kws.common.security.utils.DictUtils;
import com.kws.system.api.domain.SysDictData;
import com.kws.system.mapper.SysDictDataMapper;
import com.kws.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 * 
 * @author zhangyh
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public Page<SysDictData> selectDictDataList(QueryDictDataRq rq)
    {
        Page<SysDictData> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        return dictDataMapper.selectDictDataAll(page,rq);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public QueryDictDataInfo selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            QueryDictDataInfo data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public R createDictData(CreateDictDataRq rq) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(rq.getDictType());
        sysDictData.setDictLabel(rq.getDictLabel());
        sysDictData.setDictValue(rq.getDictValue());
        sysDictData.setStatus(rq.getStatus());
        sysDictData.setDictSort(rq.getDictSort());
        sysDictData.setRemark(rq.getRemark());
        sysDictData.setCreateBy(SecurityUtils.getStringUserId());
        sysDictData.setCreateTime(new Date());
        sysDictData.setUpdateBy(SecurityUtils.getStringUserId());
        sysDictData.setUpdateTime(new Date());
        int i = dictDataMapper.insertDictData(sysDictData);
        if (1 == i){
            return R.ok();
        }
       return R.fail();
    }
}
